/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Labsystem.Medicos;

import Entidades.Medico;
import Labsystem.DaoImpl;
import java.awt.Color;
import java.util.List;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

/**
 *
 * @author horacio
 */
public class ConsultarMedico extends javax.swing.JDialog {

    DaoImpl dao = new DaoImpl();
    
    public ConsultarMedico(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        this.setLocationRelativeTo(null);
        initComponents();
        setTitle("Consulta Medicos");
        desplegarTabla(dao.ejecutarQuery("FROM Medico"));
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tablaMedicos = new javax.swing.JTable();
        botonRegistrar = new javax.swing.JButton();
        eliminarMedico = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        textoBusqueda = new javax.swing.JTextField();
        Modificar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        tablaMedicos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tablaMedicos.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tablaMedicosMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tablaMedicos);

        botonRegistrar.setText("Registrar");
        botonRegistrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonRegistrarActionPerformed(evt);
            }
        });

        eliminarMedico.setText("Eliminar");
        eliminarMedico.setEnabled(false);
        eliminarMedico.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                eliminarMedicoActionPerformed(evt);
            }
        });

        jLabel1.setText("Buscar : ");

        textoBusqueda.setForeground(new java.awt.Color(153, 153, 153));
        textoBusqueda.setText("Nombre");
        textoBusqueda.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                textoBusquedaActionPerformed(evt);
            }
        });
        textoBusqueda.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                textoBusquedaFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                textoBusquedaFocusLost(evt);
            }
        });
        textoBusqueda.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                textoBusquedaKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                textoBusquedaKeyTyped(evt);
            }
        });

        Modificar.setText("Modificar");
        Modificar.setEnabled(false);
        Modificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ModificarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(eliminarMedico, javax.swing.GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE)
                    .addComponent(Modificar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(botonRegistrar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textoBusqueda, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 744, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(52, 52, 52)
                        .addComponent(botonRegistrar)
                        .addGap(48, 48, 48)
                        .addComponent(Modificar)
                        .addGap(44, 44, 44)
                        .addComponent(eliminarMedico)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 439, Short.MAX_VALUE)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(textoBusqueda, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(64, 64, 64))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botonRegistrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonRegistrarActionPerformed
        RegistrarMedico reg = new RegistrarMedico(this, true);
        reg.setLocationRelativeTo(this);
        reg.setVisible(true);
        desplegarTabla(dao.ejecutarQuery("FROM Medico"));
    }//GEN-LAST:event_botonRegistrarActionPerformed

    private void textoBusquedaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_textoBusquedaActionPerformed
       
    }//GEN-LAST:event_textoBusquedaActionPerformed

    private void textoBusquedaFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_textoBusquedaFocusGained
        textoBusqueda.setText("");
        textoBusqueda.setForeground(Color.BLACK);
    }//GEN-LAST:event_textoBusquedaFocusGained

    private void textoBusquedaFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_textoBusquedaFocusLost
        textoBusqueda.setText("Nombre");
        textoBusqueda.setForeground(Color.GRAY);
    }//GEN-LAST:event_textoBusquedaFocusLost

    private void tablaMedicosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tablaMedicosMouseClicked
        if(tablaMedicos.getSelectedColumnCount() > 0){
            eliminarMedico.setEnabled(true);
            Modificar.setEnabled(true);
        }
    }//GEN-LAST:event_tablaMedicosMouseClicked

    private void eliminarMedicoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_eliminarMedicoActionPerformed
        int accion = JOptionPane.showConfirmDialog(this, "¿Desea eliminar a " + tablaMedicos.getValueAt(tablaMedicos.getSelectedRow(), 0)+
                " " + tablaMedicos.getValueAt(tablaMedicos.getSelectedRow(), 1) +"?");
        if(accion == 0){
            int registro = (int) tablaMedicos.getValueAt(tablaMedicos.getSelectedRow(), 6);
            Object obj = dao.ejecutarQuery("FROM Medico where numRegistro = " + registro).get(0);
            dao.eliminar(obj);
            desplegarTabla(dao.ejecutarQuery("FROM Medico"));
        }
    }//GEN-LAST:event_eliminarMedicoActionPerformed

    private void ModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ModificarActionPerformed
        int registro = (int) tablaMedicos.getValueAt(tablaMedicos.getSelectedRow(), 6);
        Medico medico = (Medico) dao.ejecutarQuery("FROM Medico where numRegistro = " + registro).get(0);
        ModificarMedico mod = new ModificarMedico(this, true, medico);
        mod.setLocationRelativeTo(this);
        mod.setVisible(true);
        desplegarTabla(dao.ejecutarQuery("FROM Medico"));
    }//GEN-LAST:event_ModificarActionPerformed

    private void textoBusquedaKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_textoBusquedaKeyTyped
       
    }//GEN-LAST:event_textoBusquedaKeyTyped

    private void textoBusquedaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_textoBusquedaKeyReleased
       List Filtro = dao.ejecutarQuery("FROM Medico where lower(nombre) like lower('"+ textoBusqueda.getText() + "%')");
       desplegarTabla(Filtro);
    }//GEN-LAST:event_textoBusquedaKeyReleased

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ConsultarMedico.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ConsultarMedico.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ConsultarMedico.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ConsultarMedico.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                ConsultarMedico dialog = new ConsultarMedico(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    
    private void desplegarTabla(List lista) {
        
        Vector<String> tableHeaders = new Vector<String>();
        Vector tableData = new Vector();
        tableHeaders.add("Nombre");
        tableHeaders.add("Apellido");
        tableHeaders.add("Direccion");
        tableHeaders.add("Especialidad");
        tableHeaders.add("Telefono");
        tableHeaders.add("Correo");
        tableHeaders.add("Registro");
        tableHeaders.add("Cedula");
        
        for(Object obj : lista){
            Medico medico = (Medico) obj;
            Vector<Object> fila = new Vector<Object>();
            fila.add(medico.getNombre());
            fila.add(medico.getApellido());
            fila.add(medico.getDireccion());
            fila.add(medico.getEspecialidad().getDescripcion());
            fila.add(medico.getTelefono());
            fila.add(medico.getCorreo());            
            fila.add(medico.getNumRegistro());
            fila.add(medico.getCedula());
            tableData.add(fila);
        }
        
        tablaMedicos.setModel(new DefaultTableModel(tableData, tableHeaders));
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Modificar;
    private javax.swing.JButton botonRegistrar;
    private javax.swing.JButton eliminarMedico;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tablaMedicos;
    private javax.swing.JTextField textoBusqueda;
    // End of variables declaration//GEN-END:variables

   
}
